package com.jia.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.product.entity.Attr;
import com.jia.product.entity.AttrAttrGroupRelation;
import com.jia.product.entity.AttrGroup;
import com.jia.product.mapper.AttrAttrGroupRelationMapper;
import com.jia.product.mapper.AttrGroupMapper;
import com.jia.product.mapper.AttrMapper;
import com.jia.product.service.AttrGroupService;
import com.jia.product.service.AttrService;
import com.jia.product.service.CategoryService;
import com.jia.product.vo.AttrGroupWithAttrVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 属性分组(AttrGroup)表服务实现类
 *
 * @author makejava
 * @since 2025-07-08 18:15:25
 */
@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup> implements AttrGroupService {

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrAttrGroupRelationMapper attrAttrGroupRelationMapper;

    @Resource
    private AttrGroupMapper attrGroupMapper;

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private AttrService attrService;

    @Override
    public Page<AttrGroup> pageQuery(long current, long size, String key, Long catelogId) {

        Page<AttrGroup> page = new Page<>(current, size);

        QueryWrapper<AttrGroup> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or()
                        .like("attr_group_name", key);
            });
        }

        if (catelogId == 0) {
            return this.page(page, queryWrapper);
        }

        queryWrapper.eq(true, "catelog_id", catelogId);

        return this.page(page, queryWrapper);
    }

    @Override
    public AttrGroup getByIdWithPath(Long attrGroupId) {
        AttrGroup attrGroup = getById(attrGroupId);
        if (attrGroup == null) {
            return null;
        }
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return attrGroup;
    }

    @Override
    public List<Attr> listAttrByGroup(Long attrGroupId) {
        List<AttrAttrGroupRelation> relationList
                = attrAttrGroupRelationMapper.selectList(new QueryWrapper<>(
                new AttrAttrGroupRelation()).eq("attr_group_id", attrGroupId));
        List<Long> attrIds =
                relationList.stream().map(AttrAttrGroupRelation::getAttrId)
                        .collect(Collectors.toList());
        List<Attr> attrs = new ArrayList<>(attrIds.size());
        if (!CollectionUtils.isEmpty(attrIds)) {
            attrs = attrMapper.selectBatchIds(attrIds);
        }
        return attrs;
    }

    @Override
    public Page<Attr> unListAttrByGroup(long current, long size, Long attrGroupId) {
        Page<Attr> page = new Page<>(current, size);
        // 1. 分组的分类id
        AttrGroup attrGroup = attrGroupMapper.selectById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        // 2. 当前分类下 分组未选择的属性(其他分组也不能选择)
        // 1.查询分类下的其他分组(包括自己)
        List<AttrGroup> groupList = attrGroupMapper.selectList(
                new QueryWrapper<AttrGroup>().eq("catelog_id", catelogId));
        if (CollectionUtils.isEmpty(groupList)) {
            return page;
        }
        List<Long> groupIds = groupList.stream().map(AttrGroup::getAttrGroupId)
                .collect(Collectors.toList());
        // 2.查询其他分组绑定的关联
        List<AttrAttrGroupRelation> relationList = attrAttrGroupRelationMapper
                .selectList(new QueryWrapper<AttrAttrGroupRelation>()
                        .in("attr_group_id", groupIds));

        if (CollectionUtils.isEmpty(relationList)) {
            return page;
        }
        List<Long> attrsId = relationList.stream()
                .map(AttrAttrGroupRelation::getAttrId).collect(Collectors.toList());
        return attrService.page(page,
                new QueryWrapper<Attr>().eq("attr_type", 1)
                        .eq("catelog_id", catelogId)
                        .notIn("attr_id", attrsId)
        );

    }

    @Override
    public List<AttrGroupWithAttrVo> queryGroupWithAttrBy(Long categoryId) {
        List<AttrGroup> groupList = attrGroupMapper.selectList(
                new QueryWrapper<AttrGroup>().eq("catelog_id", categoryId));
        if (groupList == null || groupList.isEmpty()) {
            return Collections.emptyList();
        }
        return groupList.stream().map(group -> {
            AttrGroupWithAttrVo withAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(group, withAttrVo);
            List<Attr> attrs = listAttrByGroup(group.getAttrGroupId());
            withAttrVo.setAttrList(attrs);
            return withAttrVo;
        }).collect(Collectors.toList());
    }

}

